package com.yanqu.road.server.manger.activity.langyamemorial;

import com.yanqu.road.dao.impl.activity.langyamemorial.LangYaMemorialConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.langyamemorial.config.LangYaMemorialConfig;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.pb.activity.LangYaMemorialProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.langyamemorial.LangYaMemorialModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LangYaMemorialMgr extends TempMgr {

    private static LangYaMemorialConfig config;

    //缓存全服进度, 玩家登录时，更新
    //activityId, value
    private static Map<Integer, Long> cacheAllBuildValueMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload LangYaMemorial Activity start");
        ActivityInfo tempActivityInfo;
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.LangYaMemorial.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no LangYaMemorial Activity in show time");
            config = null;
        }else {

            tempActivityInfo = openActivityInfoList.get(0);
            int tmpActivityId = tempActivityInfo.getActivityId();

            LangYaMemorialConfig configTemp  = new LangYaMemorialConfig(tempActivityInfo);

            //config配置
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);

            LangYaMemorialConfigDaoImpl dao = new LangYaMemorialConfigDaoImpl();

            configTemp.setAtticInfoList(dao.getLangYaMemorialAtticInfoList(tmpActivityId));

            if (activityConfigMap.containsKey(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName())) {
                configTemp.setEXCHANGE_SCORE_ITEM_ID(activityConfigMap.get(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName()).getIntValue());
            }
            configTemp.setRepairProp(activityConfigMap.get(eActivityConfigType.LANGYA_PUZZLE_REPAIR_PROP.getName()).getValue());
            configTemp.setAtticConsume(activityConfigMap.get(eActivityConfigType.LANGYA_ATTIC_CONSUME.getName()).getValue());
            configTemp.setAtticIntegral(activityConfigMap.get(eActivityConfigType.LANGYA_ATTIC_INTEGRAL.getName()).getIntValue());
            configTemp.setAtticSacrificialPoints(activityConfigMap.get(eActivityConfigType.LANGYA_ATTIC_SACRIFICIAL_POINTS.getName()).getValue());
            configTemp.setGoldFloors(activityConfigMap.get(eActivityConfigType.LANGYA_ATTIC_GOLD_FLOORS.getName()).getIntValue());
            configTemp.setGoldProbability(activityConfigMap.get(eActivityConfigType.LANGYA_ATTIC_GOLD_PROBABILITY.getName()).getIntValue());
            configTemp.setAtticView(activityConfigMap.get(eActivityConfigType.LANGYA_ATTIC_VIEW.getName()).getIntValue());
            configTemp.setKongmingReward(activityConfigMap.get(eActivityConfigType.LANGYA_ATTIC_KONGMING_REWARD.getName()).getValue());
            configTemp.setAtticTenConsume(activityConfigMap.get(eActivityConfigType.LANGYA_ATTIC_TEN_CONSUME.getName()).getValue());
            configTemp.setAtticTenConsumeCond(activityConfigMap.get(eActivityConfigType.LANGYA_ATTIC_TEN_CONSUME_COND.getName()).getIntValue());
            configTemp.setATTIC_NPC_NAME(activityConfigMap.get(eActivityConfigType.LANGYA_ATTIC_NPC_NAME.getName()).getValue());
            configTemp.setLANGYA_ITEM_RETURN(activityConfigMap.get(eActivityConfigType.LANGYA_ITEM_RETURN.getName()).getValue());
            config = configTemp;
        }

        getLogger().info("reload LangYaMemorial Activity finish");

        // 在线玩家下发数据
        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            LangYaMemorialModule module = gamePlayer.getModule(LangYaMemorialModule.class);
            module.syncConfig();
            module.initUserData();
            module.syncUserData();
        }
    }

    public static void getCrossAllMakeProcess(){
        if(config == null){
            return;
        }
        if(ActivityMgr.activityOverTime(config.getActivityInfo())){
            return;
        }
        ActivityInfo activityInfo = config.getActivityInfo();
        if(cacheAllBuildValueMap.containsKey(activityInfo.getActivityId())){
            return;
        }

        LangYaMemorialProto.CrossLangYaMemorialGetAllMakeNumMgs.Builder crossSync = LangYaMemorialProto.CrossLangYaMemorialGetAllMakeNumMgs.newBuilder();
        crossSync.setActivityId(activityInfo.getActivityId());
        GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_LANGYA_MEMORIAL_GET_ALL_BUILD_VALUE, crossSync));
    }

    public static LangYaMemorialConfig getConfig() {
        return config;
    }

    public static ActivityInfo getActivityInfo(){
        if(config == null){
            return null;
        }
        return config.getActivityInfo();
    }

    public static int getActivityId(){
        ActivityInfo activityInfo = getActivityInfo();
        if(activityInfo == null){
            return 0;
        }
        return activityInfo.getActivityId();
    }

    public static Map<Integer, Long> getCacheAllBuildValueMap() {
        return cacheAllBuildValueMap;
    }
}
